//
//  LookForDetailController.m
//  CarTown
//
//  Created by xionghao on 15/9/6.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "LookForDetailController.h"
#import "NoramlCellMode.h"
#import "HeaderModel.h"
#import "ShangpuMode.h"
#import "CarlistCellMode.h"
#import "CarDetailmodel.h"
#import "ShangjiaTableViewCell.h"
#import "NoramlTableViewCell.h"
#import "CarTableCellTableViewCell.h"
#import "LookForHeaderTableViewCell.h"
#import "PeiZhiTableViewCell.h"
#import "PeizhiMode.h"
#import "Puser.h"
#import "PersonalDetailViewController.h"
#import "CompanyDetailViewController.h"
#import "CarDetailViewController.h"
#import "FindCarMode.h"
#import "FindCarResdMode.h"
#import "RootTabViewController.h"
#import "CZTabBar.h"
@interface LookForDetailController ()
@property (nonatomic,strong) NSMutableArray *dataarray;
@property (nonatomic,strong) Puser *user;
@end

@implementation LookForDetailController
+(instancetype)viewcontroller{
    LookForDetailController *vc=[[LookForDetailController alloc]initWithStyle:UITableViewStylePlain];
    return vc;
}
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}
-(NSMutableArray *)dataarray{
    if ( _dataarray==nil) {
        _dataarray=[NSMutableArray array];
    }
    return  _dataarray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    self.navigationItem.title=@"求购详情";
    UIBarButtonItem *baritem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ico_backtoindex"] style:UIBarButtonItemStyleDone target:self action:@selector(rightclick)];
    self.navigationItem.rightBarButtonItem=baritem;
    if (self.isFromSuS) {
        NSMutableArray *vcarray=[[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-2];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-3];
        self.navigationController.viewControllers = vcarray;
    }
    
    if (self.Fid) {
        [self getfindFromWeb];
    }else{
    [self setupdata];
    }
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    self.tableView.backgroundColor=bgGarycolor;

}

-(void)rightclick{
    RootTabViewController *roottab=self.navigationController.tabBarController;
    if (roottab.cztabBar.currtitem==0) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        [self.navigationController popToRootViewControllerAnimated:NO];
        [roottab.cztabBar setcheckItem:0];
        
    }

}

-(void)getfindFromWeb{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    params[@"action"]=@"id_look";
    params[@"car_num"]=@"3";
    params[@"id"]=self.Fid;
    [PostUitls Post:lookforcar parameters:params success:^(id response) {
        
        FindCarResdMode *mode=[FindCarResdMode modeWithDict:response];
        [self.dataarray removeAllObjects];
        if ([mode.state integerValue]==400) {
            self.findmode=mode.data[0];
            [self setupdata];
        }
    } failure:^(NSError *error) {
       
    }];

}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
-(void)setupdata{
    NSMutableArray *normallarray=[NSMutableArray array];
    HeaderModel *headermode=[[HeaderModel alloc]init];
    headermode.name=self.findmode.car_name;
    headermode.time=self.findmode.addtime;
    headermode.rowheight=60;
    [normallarray addObject:headermode];
    if ([self.findmode.sysprice floatValue]!=0) {
        NoramlCellMode *price=[[NoramlCellMode alloc]init];
        price.title=@"期望成交价";
        price.detail=
        [NSString stringWithFormat:@"%@万",self.findmode.sysprice];
        [normallarray addObject:price];
    }
  
    NoramlCellMode *cartype=[[NoramlCellMode alloc]init];
    cartype.title=@"车源类型";
    cartype.detail=self.findmode.car_source_type;
    [normallarray addObject:cartype];
    NoramlCellMode *color=[[NoramlCellMode alloc]init];
    color.title=@"颜色";
    color.detail=[NSString stringWithFormat:@"%@/%@",self.findmode.car_outside_color,self.findmode.car_inside_color];
    [normallarray addObject:color];
    NoramlCellMode *shangpai=[[NoramlCellMode alloc]init];
    shangpai.title=@"上牌地点";
    shangpai.detail=self.findmode.card_address;
    [normallarray addObject:shangpai];
    
    
    NoramlCellMode *kuling=[[NoramlCellMode alloc]init];
    kuling.title=@"库龄要求";
    
    switch ([self.findmode.store_age integerValue]) {
        case 0:
            kuling.detail=@"不限";
            break;
        case 3:
            kuling.detail=@"3个月内";
            break;
        case 6:
            kuling.detail=@"6个月内";
            break;
        case 12:
            kuling.detail=@"12个月内";
            break;
        default:
            break;
    }
    
   
    [normallarray addObject:kuling];
   
    NoramlCellMode *paifang=[[NoramlCellMode alloc]init];
    paifang.title=@"排放标准";
    NSString *paifstring;
    switch ([self.findmode.standard integerValue]) {
        case 0:
            paifstring=@"不限";
            break;
        case 4:
            paifstring=@"国四";
            break;
        case 5:
            paifstring=@"国五";
            break;
        default:
            break;
    }
    paifang.detail=paifstring;
    
    [normallarray addObject:paifang];
    
    if(![self.findmode.deal_address isEqualToString:@""]&&self.findmode.deal_address)
    {
        NoramlCellMode *jiaohuo=[[NoramlCellMode alloc]init];
        jiaohuo.title=@"交货地点";
        jiaohuo.detail=self.findmode.deal_address;
        [normallarray addObject:jiaohuo];
    }
    
    NoramlCellMode *jiaohuoriqi=[[NoramlCellMode alloc]init];
    jiaohuoriqi.title=@"交货日期";
    switch ([self.findmode.deal_time integerValue]) {
        case 0:
            jiaohuoriqi.detail=@"期货";
            break;
        case 3:
            jiaohuoriqi.detail=@"3天";
            break;
        case 5:
            jiaohuoriqi.detail=@"5天";
            break;
        case 7:
            jiaohuoriqi.detail=@"7天";
            break;
        case 15:
            jiaohuoriqi.detail=@"15天";
            break;
        default:
            break;
    }
    [normallarray addObject:jiaohuoriqi];
    
    
    NoramlCellMode *valitime=[[NoramlCellMode alloc]init];
    valitime.title=@"求购有效期";
    
    switch ([self.findmode.valid_time integerValue]) {
        case 3:
            valitime.detail=@"3天";
            break;
        case 5:
            valitime.detail=@"5天";
            break;
        case 7:
            valitime.detail=@"7天";
            break;
        case 15:
            valitime.detail=@"15天";
            break;
            
        default:
            break;
    }
    
    [normallarray addObject:valitime];
    
    
    if (![self.findmode.car_configure isEqualToString:@""]&&self.findmode.car_configure) {
        PeizhiMode *beizhu=[[PeizhiMode alloc]init];
        beizhu.title=@"备注";
        beizhu.longdetail=self.findmode.car_configure;
        [normallarray addObject:beizhu];
        
    }
    
    ShangpuMode *shangpu=[[ShangpuMode alloc]init];
    shangpu.shop_type=self.findmode.sale_type;
    shangpu.logo=self.findmode.ico;
    shangpu.title=self.findmode.sale_name;
    shangpu.power=[self.user.power integerValue];
    shangpu.rowheight=80;
    shangpu.phonenumber=self.findmode.phone;
    shangpu.block=^{
        int power= [self.user.power intValue];
        
        if (power==1||power==5||power==8) {
            
            if ([self.findmode.user_id integerValue]!=0) {
                PersonalDetailViewController *cdvc=[PersonalDetailViewController viewcontroller];
                cdvc.user_id=self.findmode.user_id;
                [self.navigationController pushViewController:cdvc animated:YES];
            }else{
                CompanyDetailViewController *cdvc=[CompanyDetailViewController viewcontroller];
                cdvc.com_id=self.findmode.com_id;
                cdvc.parent_id=self.findmode.parent_id;
                [self.navigationController pushViewController:cdvc animated:YES];
            }
            
            
        }else{
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            
            hud.labelText=@"请先通过认证才能查看";
            hud.mode = MBProgressHUDModeText;
            
            hud.margin = 10.f;
            
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
        }
  

    };
    [normallarray addObject:shangpu];

    [self.dataarray addObject:normallarray];

    NSMutableArray *mcararray=[NSMutableArray array];
    for (CarDetailmodel *detail in self.findmode.other_car) {
        CarlistCellMode *listcell=[[CarlistCellMode alloc]init];
        listcell.cardmode=detail;
        listcell.block=^{
            CarDetailViewController *vc=[CarDetailViewController viewcontroller];
            vc.ID=listcell.cardmode.ID;
            [self.navigationController pushViewController:vc animated:YES];
        };
        [mcararray addObject:listcell];
    }
    [self.dataarray addObject:mcararray];
    [self.tableView reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return self.dataarray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    NSArray *array=self.dataarray[section];
    
    return array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *array=self.dataarray[indexPath.section];
    NoramlCellMode *mode=array[indexPath.row];
    if ([mode isKindOfClass:[HeaderModel class]]) {
        HeaderModel *headermode=mode;
        
        LookForHeaderTableViewCell *cell=[LookForHeaderTableViewCell cellWithTableView:tableView];
        cell.titlelable.text=headermode.name;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *addtime = [NSDate dateWithTimeIntervalSince1970:[headermode.time integerValue]];
         cell.timelable.text=[dateFormatter stringFromDate:addtime];
        return cell;
    }
    else if ([mode isKindOfClass:[PeizhiMode class]])
    {
        PeizhiMode *pmode=mode;
        
        PeiZhiTableViewCell *cell=[PeiZhiTableViewCell cell];
        cell.titlelabel.text=pmode.title;
        cell.detailtext.text=pmode.longdetail;
        return cell;
    }
    else if([mode isKindOfClass:[ShangpuMode class]])
    {
        ShangjiaTableViewCell *cell=[ShangjiaTableViewCell cellWithTableView:tableView];
        cell.mode=mode;
        return cell;
    }
    else if([mode isKindOfClass:[CarlistCellMode class]]){
        CarlistCellMode *listmode=mode;
        CarTableCellTableViewCell *cell=[CarTableCellTableViewCell cellWithTableView:tableView];
        cell.carcellmode=listmode;
        return cell;
    }else{
        NoramlTableViewCell *cell=[NoramlTableViewCell cellWithTableView:tableView];
        cell.textLabel.text=mode.title;
        cell.detailTextLabel.text=mode.detail;
        return cell;
    }
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0.1;
    }else{
        return 30;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.dataarray[indexPath.section];
    NoramlCellMode *mode=array[indexPath.row];
    if (mode.rowheight!=0) {
        return mode.rowheight;
    }else{
        return 44;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.dataarray[indexPath.section];
    NoramlCellMode *mode=array[indexPath.row];
    if (mode.block) {
        mode.block();
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, XHScreenheight, 30)];
    view.backgroundColor=UIColorWithRGB(238, 238, 238);
    UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(16, 0, XHScreenheight, 30)];
    [view addSubview:lable];
    lable.font=[UIFont systemFontOfSize:14];
    lable.textColor=textnormalGary;
    if (section==0) {
        view.height=0;
    }
    else{
        lable.text=@"相关车源信息";
        view.height=30;
    }
    return view;

}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
    
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
